package cn.lf.android.lawfirm;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import cn.lf.android.lawfirm.utils.MIUIUtils;
import cn.lf.android.lawfirm.view.CToast;


/**
 * Fragment基类
 *
 * @author zhouby
 */
public abstract class BaseFragment extends Fragment implements OnClickListener {

    /**
     * 根View
     **/
    protected View rootView;

    /**
     * 获取布局资源
     **/
    protected abstract int getContentView();

    /**
     * 初始化
     **/
    protected abstract void initFragment();

    /**
     * 吐司
     **/
    protected Toast mToast;

    protected ProgressDialog bar;

    protected CToast mCToast;
    /**
     * 自定义
     **/
    protected Toast mCustomerToast;

    @Override
    public final View onCreateView(LayoutInflater inflater,
                                   ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentView(), container, false);
        initFragment();

        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 获取demin
     *
     * @param mRid
     * @return
     */
    protected int getDemonIntegerMethod(int mRid) {
        return (int) this.getResources().getDimension(mRid);
    }

    /**
     * 根据id查找View
     **/
    protected View findViewById(int id) {
        return rootView.findViewById(id);
    }

    /**
     * 取消toast
     */
    public void toastDismis() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    /**
     * 显示长Toast并且修改文字但不累加
     *
     * @param msg      String类型
     * @param isCenter 是否显示在屏幕中间
     */
    public void showLongToast(String msg, boolean isCenter) {
        showToast(msg, Toast.LENGTH_LONG, isCenter);
    }

    /**
     * 显示长Toast并且修改文字但不累加
     *
     * @param msg      int类型(R.String.)
     * @param isCenter 是否显示在屏幕中间
     */
    public void showLongToast(int msg, boolean isCenter) {
        showToast(msg, Toast.LENGTH_LONG, isCenter);
    }

    /**
     * 显示短Toast并且修改文字但不累加（一般情况调用此方法即可）
     *
     * @param msg      String类型
     * @param isCenter 是否显示在屏幕中间
     */
    public void showShortToast(String msg, boolean isCenter) {
        showToast(msg, Toast.LENGTH_SHORT, isCenter);
    }

    /**
     * 显示短Toast并且修改文字但不累加（一般情况调用此方法即可）
     *
     * @param msg      int类型(R.String.)
     * @param isCenter 是否显示在屏幕中间
     */
    public void showShortToast(int msg, boolean isCenter) {
        showToast(msg, Toast.LENGTH_SHORT, isCenter);
    }

    private void showToast(String msg, int duration, boolean isCenter) {
        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), msg, duration);
        } else {
            mToast.setText(msg);
        }
        if (isCenter) {
            mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        mToast.show();
    }

    private void showToast(int msg, int duration, boolean isCenter) {
        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), msg, duration);
        } else {
            mToast.setText(msg);
        }
        if (isCenter) {
            mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        mToast.show();
    }

    /**
     * 显示自定义ProgressBar
     */
//    public void showLoading() {
//        if (bar == null) {
//            bar = ProgressDialog.show(getActivity(), null, null);
//            bar.setContentView(R.layout.include_loading);
//        }
//    }

    /**
     * 显示默认ProgressBar
     */
    public void showProgressDia() {
        if (bar == null) {
            bar = ProgressDialog.show(getActivity(), null, null);
            bar.setContentView(new ProgressBar(getActivity()));
        }
    }

    /**
     * 取消ProgressBar
     */
    public void dismissProgressDia() {
        if (bar != null && bar.isShowing()) {
            bar.dismiss();
            bar = null;
        }
    }

    /**
     * 加载异常 在网络请求后数据异常的情况下 如500等 页面之前存在数据调用该方法,页面不存在其他数据时用showUnKnown()
     */
    public void toastUnKnown() {
        toastUnKnown(false);
        return;
    }

    /**
     * 加载异常 在网络请求后数据异常的情况下 如500等 页面之前存在数据调用该方法,页面不存在其他数据时用showUnKnown()
     *
     * @param isSubmmit 是否是提交保存的时候
     */
    public void toastUnKnown(boolean isSubmmit) {
        if (isSubmmit) {
            customShortToast(R.string.network_unkown_save_toast);
        } else {
            customShortToast(R.string.network_abnormal_toast);
        }
        return;
    }
    /**
     * 显示自定义短Toast并且修改文字但不累加（一般情况调用此方法即可）
     *
     * @param msg      int类型(R.String.)
     */
    public void customShortToast(int msg) {
        showCToast(msg);
//		customToast(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 检查网络是否可用
     * <p>
     * 该方法写在每次网络请求的点击事件监听里(无网络会Tosat提示)
     * <p>
     * true表示提交的时候判断
     * <p>
     * return true表示没有网络
     */
    public boolean checkNetWork(boolean submit) {
        if (!isNetWorkConnected()) {
            if (submit) {
                customShortToast(R.string.network_submit_toast);
            } else {
                customShortToast(R.string.network_unline_toast);
            }
            return true;
        }

        return false;
    }


    /**
     * 判断网络连接
     *
     * @return
     */
    protected boolean isNetWorkConnected() {
        ConnectivityManager conn = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net = conn.getActiveNetworkInfo();
        if (net != null && net.isConnected()) {
            return true;
        }
        return false;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 保存成功 提示
     */
//    public void showSuccessToast() {
//        Toast imageToast = new Toast(getActivity());
//        imageToast.setGravity(Gravity.CENTER, 0, 0);
//        // 定义一个InageView对象
//        ImageView imageView = new ImageView(getActivity());
//        // 为ImageView对象设置上去一张图片
//        imageView.setImageResource(R.drawable.success);
//        // 将ImageView对象绑定到Toast对象imageToasr上面去
//        imageToast.setView(imageView);
//        // 设置Toast对象显示的时间长短
//        imageToast.setDuration(Toast.LENGTH_LONG);
//        // 显示Toast
//        imageToast.show();
//    }

    public <T> T getView(@IdRes int id) {
        if (rootView == null) {
            return null;
        }
        return (T) rootView.findViewById(id);
    }

    /**
     * 1.2新加  1.5s 吐司
     *
     */
    public void showCToast(int msg) {
        permission(msg);
    }
    /**
     * 1.2新加  1.5s 吐司
     *
     * @param msg
     */
    public void showCToast(String msg) {
        permission(msg);
    }

    public void permission(String msg) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (MIUIUtils.isMIUI()) {
                customToast(msg, Toast.LENGTH_SHORT);
            } else {
                //Android6.0以上
                mCToast = CToast.makeText(getActivity(), msg, 1500);
                mCToast.show();
            }
        } else {
            //Android6.0以下，不用动态声明权限
            mCToast = CToast.makeText(getActivity(), msg, 1500);
            mCToast.show();
        }
    }


    private void customToast(String msg, int duration) {
        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.toast_bg,
                null);
        TextView text = (TextView) layout.findViewById(R.id.txt_toast);
        text.setText(msg);
        if (mCustomerToast == null) {
            mCustomerToast = new Toast(getActivity());
        }
        mCustomerToast.setGravity(Gravity.CENTER, 0, 0);
        mCustomerToast.setDuration(duration);
        mCustomerToast.setView(layout);
        mCustomerToast.show();
    }


    public void permission(int msg) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (MIUIUtils.isMIUI()) {
                customToast(msg, Toast.LENGTH_SHORT);
            } else {
                //Android6.0以上
                mCToast = CToast.makeText(getActivity(), msg, 1500);
                mCToast.show();
            }
        } else {
            //Android6.0以下，不用动态声明权限
            mCToast = CToast.makeText(getActivity(), msg, 1500);
            mCToast.show();
        }
    }
    private void customToast(int msg, int duration) {
        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.toast_bg,
                null);
        TextView text = (TextView) layout.findViewById(R.id.txt_toast);
        text.setText(msg);
        if (mCustomerToast == null) {
            mCustomerToast = new Toast(getActivity());
        }
        mCustomerToast.setGravity(Gravity.CENTER, 0, 0);
        mCustomerToast.setDuration(duration);
        mCustomerToast.setView(layout);
        mCustomerToast.show();
    }

    /**
     * 打开activity执行的方法
     */
//    @Override
//    public void startActivity(Intent intent) {
//        super.startActivity(intent);
//        getActivity().overridePendingTransition(R.anim.push_up_in, R.anim.push_none);
//    }

    /**
     * 打开activity执行的方法
     */
//    @Override
//    public void startActivityForResult(Intent intent, int requestCode) {
//        super.startActivityForResult(intent, requestCode);
//        getActivity().overridePendingTransition(R.anim.push_up_in, R.anim.push_none);
//    }

}
